package com.stu.spring04.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName: UserSelectAspect
 * Package: com.stu.spring04.aspect
 * Description: @Version 1.0
 */
@Aspect
@Component
public class UserSelectAspect {
    @Pointcut("execution(public * com.stu.spring04.service.impl.UserServiceImpl.select*(..))")
    public void myMethod(){

    }
    @Before("myMethod()")
    public void beforeMethod(JoinPoint joinPoint) throws IllegalAccessException {

        System.out.println("拦截");

    }
    @AfterReturning(value = "myMethod()" ,returning = "returnObj")
    public void afterMethod(JoinPoint joinPoint,Object returnObj) throws IllegalAccessException {
        if(returnObj instanceof ArrayList){
            for (Object eve:(ArrayList<?>) returnObj  ) {
                Class<?> aClass = eve.getClass();
                Field[] declaredFields = aClass.getDeclaredFields();
                for (Field every:declaredFields ) {
                    if (every.getName().equals("createBy")){
                        every.setAccessible(true); //暴力破解私有
                        every.set(eve,"健辉");
                    }
                    if (every.getName().equals("createTime")){
                        every.setAccessible(true); //暴力破解私有
                        every.set(eve,new Date());
                    }
                    if(every.getName().equals("password")){
                        every.setAccessible(true);
                        every.set(eve,null);
                    }
                }
            }
        }else{
            Class<?> aClass = returnObj.getClass();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field every:declaredFields ) {
                if (every.getName().equals("createBy")){
                    every.setAccessible(true); //暴力破解私有
                    every.set(returnObj,"健辉");
                }
                if (every.getName().equals("createTime")){
                    every.setAccessible(true); //暴力破解私有
                    every.set(returnObj,new Date());
                }
                if(every.getName().equals("password")){
                    every.setAccessible(true);
                    every.set(returnObj,null);
                }
            }
        }


        System.out.println("新增成功");
    }
}
